package servlet.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.VForumTitle;
import model.VUser;
import Persistant.Dao.ForumContentDAO;
import Persistant.Dao.ForumDAO;
import Persistant.Dao.ReviewDAO;
import Persistant.Dao.UserDAO;
import Persistent.factory.DAOfactory;
import Persistent.imple.ForumContentDAOImpl;
import Persistent.imple.ForumDAOImpl;
import Persistent.imple.ReviewDAOImpl;
import Persistent.imple.UserDAOImpl;

public class BlogerArtilServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String sforumid = request.getParameter("forumid");
		int forumid = 0;
		try{
		forumid = Integer.parseInt(sforumid);}
		catch(Exception e){
			forumid = 0;
		}
		//ForumDAO fdao = new ForumDAOImpl();
		//ForumContentDAO fcdao = new ForumContentDAOImpl();
		//UserDAO udao = new UserDAOImpl();
		//ReviewDAO rdao = new ReviewDAOImpl();
		
		ForumDAO fdao = DAOfactory.getForumDAO();
		ForumContentDAO fcdao = DAOfactory.getForumContentDAO();
		UserDAO udao = DAOfactory.getUserDAO();
		ReviewDAO rdao = DAOfactory.getReviewDAO();
		
		HttpSession session = request.getSession();
		HashMap<Integer, Integer>scanforumlist = (HashMap<Integer, Integer>)session.getAttribute("scanforumlist");
		
		if(!scanforumlist.containsKey(forumid)){
			fdao.increaseScanCounter(forumid);
			scanforumlist.put(forumid, forumid);
			session.setAttribute("scanforumlist", scanforumlist);
		}
		
		
		
		VForumTitle forum = fdao.getVForumById(forumid);
		VUser blogeruser = udao.getVUserById(forum.getUserid());
		List list = fcdao.getContentListByForumId(forumid);
		List reviewlist = rdao.getReviewlListByForumId(forumid);
		
		request.setAttribute("forum", forum);
		request.setAttribute("blogeruser", blogeruser);
		request.setAttribute("contentlist", list);
		request.setAttribute("reviewlist", reviewlist);
		RequestDispatcher rd = request.getRequestDispatcher("blogerarticle.jsp");
		rd.forward(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
